using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComProduct.Costing.DS;

namespace PCSComProduct.Items.BO
{
	public class ItemCostDetailBO //: IItemCostDetailBO
	{
		private const string THIS = "PCSComProduct.Items.BO.ItemCostDetailBO";
		public ItemCostDetailBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}


	
		public void Add(object pObjectDetail)
		{
			// TODO:  Add ItemCostDetailBO.Add implementation

		}
	
		public void Delete(object pObjectVO)
		{
			// TODO:  Add ItemCostDetailBO.Delete implementation

		}
	
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add ItemCostDetailBO.GetObjectVO implementation
			return null;
		}
	
		public void Update(object pObjectDetail)
		{
			// TODO:  Add ItemCostDetailBO.Update implementation

		}
	
		public void UpdateDataSet(DataSet dstData)
		{
			// TODO:  Add ItemCostDetailBO.UpdateDataSet implementation

		}
		#region IItemCostDetailBO Members
		//**************************************************************************              
		///    <Description>
		///    Get the Cost information
		///    1.Cost Description
		///    2. and the order cost
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///       Button will be disable or enable
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       THIENHD
		///    </Authors>
		///    <History>
		///       15-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		//[AutoComplete(true)]		
		public DataTable GetListOfCostDetail(int pintProductID, int pintCCNID)
		{
			const int MAX_ROW_NUMBER = 21;
			try 
			{
				//1.Get the ITM_CostDescription
				ITM_CostDescriptionDS objITM_CostDescriptionDS = new ITM_CostDescriptionDS();
				DataTable dtCost_Description = objITM_CostDescriptionDS.GetCostDescription(pintCCNID);

				//2.Get the ITM_CostOperation
				ITM_CostDS objITM_CostDS = new ITM_CostDS();
				DataTable dtCostOperation = objITM_CostDS.GetCostOperation(pintProductID,pintCCNID);

				//3.Define the column name for this table
				const string COSTDESCRIPTION_FLD = "CostDescription";
				const string COMPONENTCOST_FLD = "ComponentCost";
				const string VALUEADDEDCOST_FLD = "ValueAddedCost";
				const string ITEMCOST_FLD = "ItemCost";
				const string COSTDESC_PREFIX_FLD = "CostDesc";

				const string COM_PREFIX = "Com";
				const string VADD_PREFIX = "VAdd";
				const string ITEM_PREFIX = "Item";

				//Create this table
				DataTable dtResult = new DataTable();
				dtResult.Columns.Add(COSTDESCRIPTION_FLD,System.Type.GetType("System.String"));
				dtResult.Columns.Add(COMPONENTCOST_FLD,System.Type.GetType("System.Decimal"));
				dtResult.Columns.Add(VALUEADDEDCOST_FLD,System.Type.GetType("System.Decimal"));
				dtResult.Columns.Add(ITEMCOST_FLD,System.Type.GetType("System.Decimal"));
				DataRow drNewRow;

				//Add default 21 empty row
				for (int i=1 ; i<=MAX_ROW_NUMBER; i++)
				{
					drNewRow = dtResult.NewRow();
					dtResult.Rows.Add(drNewRow);
				}

				bool blnHaveData = false;

				//first add the Cost Description to the 21 rows
				if (dtCost_Description.Rows.Count > 0)
				{
					blnHaveData = true;
					for (int i=1 ; i<=21; i++)
					{
						string strCostDescName = COSTDESC_PREFIX_FLD + i.ToString().PadLeft(2,'0');
						dtResult.Rows[i-1][COSTDESCRIPTION_FLD] = dtCost_Description.Rows[0][strCostDescName];
					}
				}

				//add the component cost, valued added cost, item cost
				if (dtCostOperation.Rows.Count > 0) 
				{
					blnHaveData = true;
					//assign the Component Cost
					for (int i=0; i<dtCostOperation.Columns.Count ; i++)
					{
						//get the column name
						string strColumnName = dtCostOperation.Columns[i].ColumnName;
						//get the last number of the column
						int intColumnSubfix  = int.Parse(strColumnName.Substring(strColumnName.Length - 2,2));
						//compare to know which column name comes to which field
						if (strColumnName.StartsWith(COM_PREFIX))
						{
							dtResult.Rows[intColumnSubfix-1][COMPONENTCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
						else if (strColumnName.StartsWith(VADD_PREFIX))
						{
							dtResult.Rows[intColumnSubfix-1][VALUEADDEDCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
						else if (strColumnName.StartsWith(ITEM_PREFIX))
						{
							dtResult.Rows[intColumnSubfix-1][ITEMCOST_FLD] = dtCostOperation.Rows[0][strColumnName];
						}
					}

				}
				if (!blnHaveData)
				{
					dtResult.Rows.Clear();
				}
				
				
				return dtResult;
			}
			catch (PCSDBException ex) 
			{
				throw ex;
			}
			catch (Exception ex) 
			{
				throw ex;
			}
		}

		#endregion
	}
}
